package org.robot.scm.pojos.platform;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.enums.ChannelEnum;
import org.robot.scm.pojos.enums.HttpMethodEnum;
import org.robot.scm.pojos.enums.MessageTypeEnum;

@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class FuturesContractsBaseNotify extends BaseNotify {

    private MessageTypeEnum messageType;

    private String htmlMessage;

    private String textMessage;

    private String deviceSn;

    private String topic;

    @Override
    public TargetNotify barkToNotifyBean(String deviceSn, BaseConfig config) {
        return TargetNotify.builder()
                .channelEnum(ChannelEnum.BARK)
                .notifyUrl(config.get().getNotifyChannel().getBark())
                .deviceSn(deviceSn)
                .topic(StringUtils.isBlank(topic) ? "消息提醒" : topic)
                .content(textMessage)
                .detailUrl("")
                .icon(config.getCurrentPlatform().getIconUrl())
                .build();
    }

    @Override
    public TargetNotify pushmeToNotifyBean(String deviceSn, BaseConfig config) {
        MessageTypeEnum messageTypeEnum = Objects.isNull(messageType) ? MessageTypeEnum.TEXT : messageType;
        String type;
        String message;
        switch (messageTypeEnum) {
            case MARKDOWN:
                type = "markdown";
                message = textMessage;
                break;
            case HTML:
                type = "html";
                message = htmlMessage;
                break;
            case TEXT:
            default:
                message = textMessage;
                type = "text";
                break;
        }
        String notifiedUrl = config.get().getNotifyChannel().getPushme() + "&type=" + type;
        return TargetNotify.builder()
                .httpMethodEnum(HttpMethodEnum.GET)
                .channelEnum(ChannelEnum.PUSHME)
                .notifyUrl(notifiedUrl)
                .topic(StringUtils.isBlank(topic) ? "消息提醒" : topic)
                .content(message)
                // 为数字的时候是授权码
                .deviceSn(StringUtils.isNumeric(this.deviceSn) ? this.deviceSn : deviceSn)
                .build();
    }

    @SneakyThrows
    @Override
    public TargetNotify robotToNotifyBean(String robotId, BaseConfig config) {
        String content = config.robotText(robotId, URLEncoder.encode(textMessage, StandardCharsets.UTF_8));
        return TargetNotify.builder()
                .httpMethodEnum(HttpMethodEnum.POST)
                .channelEnum(ChannelEnum.ROBOT)
                .notifyUrl(config.robotUrl())
                .topic(config.get().getNotifyChannel().getRobot().getBodyText())
                .content(content)
                .deviceSn(deviceSn)
                .build();
    }

    @Override
    public boolean equal(BaseNotify another) {
        return false;
    }

}
